import {Component} from '@angular/core';
import {Observable} from "rxjs";
import {select, Store} from "@ngrx/store";
import {increment, decrement, reset} from '../store/actions/counter.actions';
import {counterState} from "../store/reducers/counter.reducers";
import {Post} from "../store/models/post.model";
import {Service} from "../service/service";
import {entityAdapter, PostEntity} from "../store/reducers/post.reducer";
import {PostActions} from "../store/actions/post.actions";

@Component({
  selector: 'app-testB',
  templateUrl: './testB.component.html',
  styleUrls: ['./testB.component.less']
})
export class TestBComponent {
  count$: Observable<counterState>;
  // posts$: Observable<Post[]>;
  test$: any
  testSubscription: any
  postList: PostEntity[] | undefined;
  addDisabled: boolean = false;

  constructor(
    private store: Store<{ counter: counterState, post:  { entities: PostEntity[] }}>,
              private service: Service) {
    this.count$ = store.select('counter');
    // this.posts$ = store.pipe(select('posts', 'entities'));
    store.pipe(select('post')).subscribe(res=>{
      console.log(res)
      this.postList = Object.values(res.entities)
    })
    // entityAdapter.getSelectors().selectAll()
  }

  increment() {
    this.store.dispatch(increment({num: 5}));
  }

  decrement() {
    this.store.dispatch(decrement({num: 5}));
  }

  reset() {
    this.store.dispatch(reset());
  }

  add() {
    this.store.dispatch(PostActions.addEntity({
      data: {
        title: 'title 4',
        name: 'name 4',
        content: 'content 4'
      }
    }))
    this.addDisabled = true
  }
}
